package org.zjsru.order.mapper;

import org.zjsru.order.pojo.Order;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.time.LocalDateTime;
import java.util.List;

public interface OrderMapper {

    @Select("select * from tb_order where id = #{id}")
    Order findById(Long id);
    
    @Select("select * from tb_order where user_id = #{userId} order by create_time desc")
    List<Order> findByUserId(@Param("userId") Long userId);
    
    @Insert("insert into tb_order (order_no, user_id, product_id, product_name, price, quantity, " +
            "total_amount, status, address, create_time, update_time) " +
            "values (#{orderNo}, #{userId}, #{productId}, #{productName}, #{price}, #{quantity}, " +
            "#{totalAmount}, #{status}, #{address}, #{createTime}, #{updateTime})")
    int insert(Order order);
    
    @Update("update tb_order set status = #{status}, update_time = #{updateTime}, " +
            "pay_time = #{payTime} where id = #{id}")
    int update(Order order);
    
    @Select("select * from tb_order where status = 0 and create_time < #{timeoutTime}")
    List<Order> findTimeoutOrders(@Param("timeoutTime") LocalDateTime timeoutTime);
}
